#ifndef SOCKET_DISPATCH_H
#define SOCKET_DISPATCH_H

#include "Reactor.hpp"
#include "Connection.hpp"
#include "../base/Defind.hpp"

namespace DBServer
{
namespace Socket
{

class Dispatch : public Reactor{
public:
    Dispatch(Listener&);
    ~Dispatch();

    /* run and dispatch the connected connection to reactor */
    int run(std::function<std::string(std::string&&)>);
    int stop();

    /* register a reactor in the dispatch */
    int reactor_reg(Reactor*);  

    /* register a listen */
    int register_read(Listener*);
    int delete_event(Listener*);

    int get_listener_fd();
    int get_rig_reactos();

    // not allow to be copied
    Dispatch& operator=(const Dispatch&) = delete;
    Dispatch&& operator=(const Dispatch&&) = delete;
    Dispatch(const Dispatch&) = delete;
    Dispatch(const Dispatch&&) = delete;
private:
    /* dispatch the connection for the reactor */
    int dispatch_conn(std::shared_ptr<ServerConn> conn, int& basic);

private:
    // don't need this function 
    // because it create a epoll fd in the parent constructer
    int reactor_init(Listener*) { return 0; }
    int register_read(Connection*) { return 0; }
    int register_write(Connection*) { return 0; }
    int delete_event(Connection*) { return 0; }
    int get_epoll_fd() { return 0; }
    int get_socket_num() { return 0; }
    void add_conn(std::shared_ptr<ServerConn>) { }

private:
    Listener* listener;
    int status;
    int rig_reactors;
    Reactor* reactors[REACTOR_NUM];
    bool is_stop;
};
    
} // namespace Socket    
} // namespace DBServer



#endif